package com.example.cc.client.processor;

import com.example.cc.client.manager.GenericCacheManager;
import com.example.cc.client.service.ClientConfigService;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

import java.util.Map;

/**
 * 这个时机太早了
 */
@Deprecated
@NoArgsConstructor
@Slf4j
public class ConfigSyncBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    @Autowired
    private ClientConfigService configService;

    @Autowired
    private GenericCacheManager<String, Object> cacheManager;

    @Override
    public void postProcessBeanFactory(@NonNull ConfigurableListableBeanFactory beanFactory) throws BeansException {
        trySyncConfiguration();
    }

    private void trySyncConfiguration() {
        try {
            Map<String, Object> configurations = configService.loadAppAllConfig();// 这个时候还是空的
            cacheManager.updateCache(configurations);
            log.info("Configuration synchronized successfully.");
        } catch (Exception e) {
            log.error("Failed to synchronize configurations.", e);
        }
    }
}
